<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category QueryList.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/2  14:45
 * +--------------------------------------------------------+
 */

namespace App\Services\QueryList;


use App\Models\CollectArticle;
use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;

class QueryList
{

    /**
     * 保存类实例的私有静态成员变量
     * 此处有问题：在homestead中，私有属性继承出错，修改为protected。其他环境（phpstudy,lnmp一键）没问题
     * @var $instance
     */
    private static $instance;

    /**
     * @return static
     * @Depiction 对外提供获取唯一实例的方法
     * @Author hwy <429208948@qq.com>
     * @Date 2022/6/17  19:26
     */
    public static function getInstance(): QueryList
    {
        //检测是否被实例化
        if (static::$instance instanceof static) {
            return static::$instance;
        }
        static::$instance = new static();
        return static::$instance;
    }

    /**
     * BaseService constructor.
     * 定义一个私有的构造函数，确保单例类不能通过new关键字实例化，只能被其自身实例化
     */
    private function __construct()
    {
    }


    /**
     * @param string $url
     * @param array $reg
     * @param string $rang
     * @return array
     * @Depiction 采集
     * @Author hwy <429208948@qq.com>
     * @Date 2022/6/17  18:52
     */
    public function collect(string $url, array $reg, string $rang)
    {
        try {
            $ql = \QL\QueryList::get($url)->rules($reg)->range($rang)->query();

            $data = $ql->getData();
            $data = $data->all();
        } catch (\Exception $exception) {
            $data = [];
        }

        return $data;
    }


}
